Skip to content

fix(web): close opened upload files when a later file fails to open - #2078

Merged
WilliamBergamin merged 1 commit into
mainfrom
fix/async-upload-file-leak
Nov 23, 2026
Merged

WilliamBergamin merged 1 commit into
mainfrom
fix/async-upload-file-leak

Conversation

@WilliamBergamin

Copy link
Copy Markdown
Contributor

Summary

This pull request makes the async and legacy WebClients close files they already opened for an upload when a later file path fails to open.

  • _files_to_data opened each string path and kept the open files in a local list, and both _send methods called it before their try. So if the second open() raised (e.g. FileNotFoundError), the first file stayed open until garbage collection, with a ResourceWarning. A public way to hit it: files_remote_add / files_remote_update with both preview_image and indexable_file_contents given as paths.
  • The helper now opens files on a contextlib.ExitStack and returns it with pop_all(). A failed open() closes the earlier files, and on success the caller closes them with with _files_to_data(req_args):, replacing the hand-written finally loop in both clients.
  • The sync BaseClient is unaffected: it opens files inside its try and tracks each one before opening the next.

Testing

tests/slack_sdk_async/web/test_async_internal_utils.py passes one existing and one missing path and checks that the first file is closed after FileNotFoundError. It fails on main and passes here. A second test checks the files stay open inside the with and close after it. The web client tests also pass on Python 3.7.

Category

  • slack_sdk.web.WebClient (sync/async) (Web API client)
  • slack_sdk.webhook.WebhookClient (sync/async) (Incoming Webhook, response_url sender)
  • slack_sdk.socket_mode (Socket Mode client)
  • slack_sdk.signature (Request Signature Verifier)
  • slack_sdk.oauth (OAuth Flow Utilities)
  • slack_sdk.models (UI component builders)
  • slack_sdk.scim (SCIM API client)
  • slack_sdk.audit_logs (Audit Logs API client)
  • slack_sdk.rtm_v2 (RTM client)
  • /docs (Documents)
  • /tutorial (PythOnBoardingBot tutorial)
  • tests/integration_tests (Automated tests for this library)

Requirements

  • I've read and understood the Contributing Guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I have signed the Salesforce CLA (required before your PR can be reviewed or merged).
  • I've run python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.sh after making the changes.

🤖 Generated with Claude Code

The async and legacy clients opened upload files in `_files_to_data`
before their `try`, and the helper kept the open files in a local list.
If a later path failed to open, the files opened before it were never
closed by the SDK. The helper now opens them on an `ExitStack` and hands
it to the caller, which closes them with `with`.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.71%. Comparing base (2c72986) to head (6d39675).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2078   +/-   ##
=======================================
  Coverage   84.71%   84.71%           
=======================================
  Files         118      118           
  Lines       14122    14116    -6     
=======================================
- Hits        11963    11959    -4     
+ Misses       2159     2157    -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@WilliamBergamin WilliamBergamin self-assigned this Nov 20, 2026
@WilliamBergamin
WilliamBergamin marked this pull request as ready for review November 20, 2026 19:15
@WilliamBergamin
WilliamBergamin requested a review from a team as a code owner November 20, 2026 19:15
@WilliamBergamin WilliamBergamin added bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented semver:patch web-client labels Nov 20, 2026
@WilliamBergamin WilliamBergamin added this to the 3.48.1 milestone Nov 20, 2026

@zimeg zimeg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐍 @WilliamBergamin LGTM! The ExitStack with pop_all() is a neat way to close earlier files on a failed open, and the tests cover both paths.

@WilliamBergamin
WilliamBergamin merged commit 80f2f22 into main Nov 23, 2026
19 checks passed
@WilliamBergamin
WilliamBergamin deleted the fix/async-upload-file-leak branch November 23, 2026 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented semver:patch web-client

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants